using System;
using System.IO;
using System.Windows.Forms;
using SpellCenter.Core;
using SpellCenter.UI.Properties;

namespace SpellCenter.UI.FileChoosing
{
    public partial class FileChooserControl : UserControl
    {
        private Control[] _pages;

        /// <summary>
        /// this page number keeps track of which page we are currently on, BackNextCancelControl.PageNumber
        /// keeps track of which page we should be on.
        /// </summary>
        private int _pageNumber;

        public FileChooserControl()
        {
            InitializeComponent();
            SetupPages();
            RegisterEvents();
            SetPage();
        }

        private void SetupPages()
        {
            _pages = new Control[] { _savePage, _fileSelectPage };
            _pageNumber = 0;
            _savePage.PageId = 0;
            _pageChanger.AddValidator(_savePage, 0);
            SetPage();
        }

        #region Events

        private void RegisterEvents()
        {
            _pageChanger.Cancel += PageChanger_Cancel;
            _pageChanger.PageChanged += PageChanger_PageChanged;
        }

        private void PageChanger_PageChanged(object sender, EventArgs e)
        {
            SetPage();
        }

        private void PageChanger_Cancel(object sender, EventArgs e)
        {
            OnCancel(EventArgs.Empty);
        }

        public event EventHandler Cancel;

        protected void OnCancel(EventArgs e)
        {
            EventHandler handler = Cancel;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler Closing;

        protected void OnClosing(EventArgs e)
        {
            EventHandler handler = Closing;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        private void SetPage()
        {
            if (_pageChanger.PageNumber >= _pages.Length)
                Finish();
            else
            {
                _pages[_pageNumber].Visible = false;
                _pages[_pageChanger.PageNumber].Visible = true;
                _pageNumber = _pageChanger.PageNumber;
            }
        }

        private void Finish()
        {
//todo            MakeCorpusConfig();
            OnClosing(EventArgs.Empty);
        }

        private FilesInCorpusList _result;

        public FilesInCorpusList Result
        {
            get { return _result; }
        }

        public string DictionaryFile
        {
            get { return _savePage.DictionaryFile; }
        }

      //todo move to pm
      //private void MakeCorpusConfig()
        //{
        //    FilesInCorpusList config = new FilesInCorpusList();
        //    foreach (FileInfo file in _fileSelectPage.IncludedFiles)
        //    {
        //        config.IncludePatterns.Add(new FilePattern(file.DirectoryName, file.Name, false));
        //    }
        //    foreach (FileInfo file in _fileSelectPage.ExcludedFiles)
        //    {
        //        config.ExcludePatterns.Add(new FilePattern(file.DirectoryName, file.Name, false));
        //    }
        //    config.WritingSystemId = _savePage.WritingSystemID;

        //    config.SaveAs();
        //    _result = config;
        //    Settings.Default.LastOpenProject = _result.Filename;
        //    Settings.Default.Save();
        //}

        public void Reset(FilesInCorpusList config)
        {
            _pageChanger.Reset();
            if(config != null)
            {
                _savePage.WritingSystemID = config.WritingSystemId;
                _fileSelectPage.CorpusConfig = config;
            }
            SetPage();
        }
    }
}
